<?php
	class Login extends Controller{
		function Login(){
			parent::Controller();
		}
		function index(){
			if($_SERVER['REQUEST_METHOD']=="GET"){
				$this->load->view('admin/login');
				return;
			}
			else{
				$username=$this->input->post('username');
				$password=$this->input->post('password');
				$reg_username="/^\w{5,16}$/";
				$reg_password="/^.{5,32}$/";
				if(!preg_match($reg_username,$username) || !preg_match($reg_password,$password)){
					$this->login_fail();
					return;
				}
				$password=md5($password);
				$this->load->database();
				$result=$this->db->select('count(*) as recordcount')->where('username',$username)->where('password',$password)->get('manager')->row()->recordcount;
				if($result==1){
					$this->load->library('session');
					$arr=array('username'=>$username);
					$this->session->set_userdata($arr);
					header('location:'.site_url('admin'));
					return;
				}
				else{
					$this->login_fail();
					return;
				}
			}
		}
		function login_fail(){
			$data['data']=array(
				'flag'=>0,
				'message'=>'登录失败,用户名或密码错误!',
				'link'=>'返回登录',
				'href'=>site_url('admin/login')
			);
			$this->load->view('admin/show_info',$data);
		}
	}
?>